package com.app.service.impl;

import com.app.mapper.MessageMapper;
import com.app.mapper.MessageReadMapper;
import com.app.mapper.TypeMapper;
import com.app.model.MessageModel;
import com.app.model.MessageReadModel;
import com.app.service.MessageReadService;
import com.app.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class MessageReadServiceImpl implements MessageReadService {
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    MessageReadMapper messageReadMapper;

    @Override
    public MessageReadModel getReadByReadId(int id, Integer personId) {
        QueryWrapper<MessageReadModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("message_id", id).eq("person_id", personId);
        MessageReadModel messageModel = messageReadMapper.selectOne(queryWrapper);
        if (messageModel == null) {
            return null;
        } else {
            return messageModel;
        }
    }

    @Override
    public List<MessageReadModel> getMesListByPersonId(Integer personId) {
        QueryWrapper<MessageReadModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("person_id", personId);
        List<MessageReadModel> readModelsList = messageReadMapper.selectList(queryWrapper);
        return readModelsList;
    }

//    @Override
//    public  Page<MessageModel> getPageMesList(Integer page,Integer limit,String title) {
//        QueryWrapper<MessageModel> queryWrapper = new QueryWrapper<>();
////        queryWrapper.select();
//        if(title != null){
//            queryWrapper.select().like("title",title);
//        }
//        queryWrapper.orderByAsc("id");
//        Page<MessageModel> pages = new Page<>();
//        pages.setCurrent(page);
//        pages.setSize(limit);
//        Page<MessageModel> messageModelPage = messageMapper.selectPage(pages,queryWrapper);
//        return messageModelPage;
//    }

//    @Override
//    public List<MessageModel> getMesList(List<Integer> idList) {
//        QueryWrapper<MessageModel> queryWrapper = new QueryWrapper<>();
//        if(idList.size()>0){
//            for (int i = 0; i < idList.size(); i++) {
//                queryWrapper.select().eq("department_id",idList.get(i)).eq("status",1);
//            }
//        }{
//            queryWrapper.select().eq("status",1);
//        }
//
//        queryWrapper.orderByAsc("id");
//        List<TypeModel> typeModelList = typeMapper.selectList(queryWrapper);
//        return typeModelList;
//    }


    @Override
    public void update(MessageReadModel model) {
        messageReadMapper.updateById(model);
    }

    @Override
    public void insert(MessageReadModel model) {
        messageReadMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        messageReadMapper.deleteById(id);
    }

    @Override
    public List<MessageReadModel> selectReadList() {
        return messageReadMapper.selectList(null);
    }

    @Override
    public void deleteByMsId(Integer id) {
        QueryWrapper<MessageReadModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("message_id", id);
        messageReadMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByMsIdAndPersonId(Integer id, Integer id1) {
        QueryWrapper<MessageReadModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("message_id", id);
        queryWrapper.eq("person_id", id1);
        messageReadMapper.delete(queryWrapper);
    }
}
